package naga.x.platform2.utils;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;

public class LRUSet<E> extends AbstractSet<E> {

	private Map<E, Object> map;
	private static final Object PRESENT = new Object();
	
	public LRUSet() {
		this(Integer.MAX_VALUE);
	}
	
	public LRUSet(int max) {
		this(16, 0.75f, max);
	}
	
	public LRUSet(int initialCapacity, float loadFactor, int max) {
		map = new LRUCache<E, Object>(initialCapacity, loadFactor, max);
	}
	
	public Iterator<E> iterator() {
        return map.keySet().iterator();
    }

    public int size() {
        return map.size();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public boolean contains(Object o) {
        return map.containsKey(o);
    }

    public boolean add(E e) {
        return map.put(e, PRESENT) == null;
    }

    public boolean remove(Object o) {
        return map.remove(o) == PRESENT;
    }

    public void clear() {
        map.clear();
    }

}
